package com.corn.juc.bq;

import lombok.SneakyThrows;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2020/12/31 16:43
 */

public class SynchronousQueueDemo {

    /**
     * put 操作
     * take 0
     * put 操作
     * take 1
     * put 操作
     * take 2
     *
     * @param args
     */
    public static void main(String[] args) {
        SynchronousQueue<Integer> queue = new SynchronousQueue<>();
        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    queue.put(i);
                    System.out.println("put 操作");
                    TimeUnit.SECONDS.sleep(2);
                }
            }
        }.start();

        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println("take " + queue.take());
                }
            }
        }.start();
    }
}
